package com.cn;

import org.flowable.engine.*;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2月15号
 * 测试
 */
@SpringBootTest
public class FlowableDemo03 {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private HistoryService historyService;


    @Test
    void test01(){
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey("approval").list();
        System.out.println(list);
        List<ProcessDefinition> pages = repositoryService.createProcessDefinitionQuery().processDefinitionKey("approval").listPage(1, 30);
        System.out.println(pages);

    }

    /**
     * 查询制定流程实力
     */
    @Test
    void test02(){
        //act_re_pr 中的 key
        String processDefinitionKey= "approval";
        List<Execution> executions = runtimeService.createExecutionQuery().processDefinitionKey(processDefinitionKey).list();
        List<Execution> executionPages = runtimeService.createExecutionQuery().processDefinitionKey(processDefinitionKey).listPage(1, 30);
        System.out.println(executionPages);
        System.out.println(executions);
    }

    @Test
    void test03(){
        /*
         * @Date: 2021/10/17 23:40
         * Step 5: 学生查询可以操作的任务,并完成任务
         */
        String candidateGroup = "stu_group"; //候选组 xml文件里面的 flowable:candidateGroups="stu_group"
        List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).orderByTaskCreateTime().desc().list();
        for (Task task : taskList) {
            // 申领任务
            taskService.claim(task.getId(), "my");
            // 完成
            taskService.complete(task.getId());
        }
        System.out.println(taskList);

    }

    /**
     *
     */
    @Test
    void test04(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("num",5);
        taskService.complete("78b46c22-ced5-11ee-894b-30d04231d1ff",map);
    }

}
